package com.example.rjb_shoping.service.impl;

import com.example.rjb_shoping.config.RjbConstant;
import com.example.rjb_shoping.entity.RjbRule;
import com.example.rjb_shoping.dao.RjbRuleDao;
import com.example.rjb_shoping.service.RjbRuleService;
import com.example.rjb_shoping.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * (RjbRule)表服务实现类
 *
 * @author makejava
 * @since 2021-06-09 20:43:19
 */
@Slf4j
@Service("rjbRuleService")
public class RjbRuleServiceImpl implements RjbRuleService {
    @Resource
    private RjbRuleDao rjbRuleDao;
    @Resource
    private RedisUtils redisUtils;
    @PostConstruct
    public void loadDiscountData() {
        List<RjbRule> rjbRules = rjbRuleDao.loadDiscountRule();
        log.info("discountRule>>" + rjbRules);
        //将打折规则载入到redis库中
        redisUtils.set(RjbConstant.SECKILL_DISCOUNT, rjbRules);
    }

    @Override
    public RjbRule queryById(Integer rId) {
        return this.rjbRuleDao.queryById(rId);
    }

    @Override
    public List<RjbRule> queryAllByLimit(int offset, int limit) {
        return this.rjbRuleDao.queryAllByLimit(offset, limit);
    }

    @Override
    public RjbRule insert(RjbRule rjbRule) {
        this.rjbRuleDao.insert(rjbRule);
        return rjbRule;
    }

    @Override
    public RjbRule update(RjbRule rjbRule) {
        this.rjbRuleDao.update(rjbRule);
        return this.queryById(rjbRule.getRId());
    }

    @Override
    public boolean deleteById(Integer rId) {
        return this.rjbRuleDao.deleteById(rId) > 0;
    }

    @Override
    public List<RjbRule> queryAllList() {
        return rjbRuleDao.queryAllList();
    }
}
